package com.yidu.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * <p>@Description: </p>
 * <p>@author yidu-Zp</p>
 * <p>@Date: 2021/1/5</p>
 * <p>@Time: 13:54</p>
 */
public class SqlSessionsTools {
    public static SqlSession getSession() {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //连接工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //从工厂里面取出一个活得链接
        SqlSession session = sqlSessionFactory.openSession();

        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return session;
    }
}
